<template>
  <section>
      <table style="width:100%;">
          <tr>
              <td style="width:200px;" valign="top">
                  <div>所属分类</div>
                  <el-tree :data="catList" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
              </td>
              <td valign="top">
                  <div>{{ catInfo.name }}</div>
                  <table class="ikd-input-table ik-table-layout-fixed">
                    <thead>
                    <tr>
                        <th nowrap></th>
                        <th nowrap>所属分类</th>
                        <th nowrap>品牌LOGO</th>
                        <th nowrap>品牌名称</th>
                        <th nowrap style="width:100px;">排序</th>
                        <th nowrap style="width:100px;">是否显示</th>
                    </tr>
                    </thead>
                    <tbody>
                        <template v-for="(item, idx) in list">
                            <tr :key="item.id">
                                <td nowrap>
                                    {{ idx+1 }}
                                </td>
                                <td>{{ item.categoryName }}</td>
                                <td><pic-view v-model="item.brandPicture"></pic-view></td>
                                <td>{{ item.brandName }}</td>
                                <td><el-input size="mini" type="number" v-model="item.orderNum" /></td>
                                <td>
                                    <el-switch
                                        :key="item.id"
                                        :active-value="1"
                                        :inactive-value="0"
                                        :value="item.status"
                                        @change="setShow(item)"
                                    ></el-switch>
                                    <!-- <el-button size="mini" @click="setShow(item)">{{item.status==1?'隐藏':'显示'}}</el-button> -->
                                </td>
                            </tr>
                        </template>
                    </tbody>
                </table>
                <div v-if="catInfo.id>0">
                    <el-button size="mini" type="primary" @click="handleSave">保存</el-button>
                </div>
              </td>
          </tr>
      </table>
  </section>
</template>

<script>
    import {
        isIntegerNotMust
    } from '../../utils/validator'
    import {
        retail_brand_ad_list,
        retail_brand_ad_save,
        retail_brand_cat_all
    } from '../../api/retail_product';
    export default {
        components: {
            'pic-view': () => import('../../components/PicView/index'),
        },
        data() {
            return {
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                catList: [],
                list: [],
                catInfo: {},
            }
        },
        mounted() {
        },
        created() {
            this.getCategoryList();
        },
        methods: {
            setShow(item) {
                // console.log(item)
                item.status = (item.status == 1) ? 0 : 1;
                // console.log(item)
            },
            handleNodeClick(data) {
                this.catInfo = data;
                this.getList();
                // console.log(data);
            },
            getCategoryList() {
                retail_brand_cat_all().then((res)=>{
                    this.catList = res.data;

                    if(this.catList && this.catList.length > 0) {
                        this.catInfo = this.catList[0];
                        this.getList();
                    }
                }).catch((err)=>{

                })
            },
            getList() {
                if(this.catInfo && this.catInfo.id > 0) {
                    retail_brand_ad_list(this.catInfo.id).then((res)=>{
                        this.list = res.data
                    }).catch((err)=>{

                    })

                } else {
                    this.list = [];
                }
            },
            handleSave() {
                this.$confirm('确定保存?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        retail_brand_ad_save({
                            list: this.list
                        })
                            .then(() => {
                                this.$message.success("保存成功");
                                this.getList();
                            });
                    }).catch(()=>{});
            }
        }
    };
</script>
<style lang="scss" scoped>
</style>
